*************************************************************************
*																		*
*					Campaign Panel Book Chapter 6 - Michigan Variables	*
*																		*
*************************************************************************	

use "${data}\scalometer government 2009.dta", clear

rename lfdn09 lfdn
rename a_kp4_730 kp4_730

save "${data}\scalometer government 2009.dta", replace


use "${data}\ZA5305_en_v5-0-0.dta", clear

********************************************************************************
** East-West
********************************************************************************

tab fedstate
tab fedstate, nol
gen ost1=0
replace ost1=1 if fedstate==3 | fedstate==4 | fedstate==8 | fedstate==13 | fedstate==14 | fedstate==16
tab1 ost*

foreach welle of numlist 2/7 {
	gen ost`welle'=ost1
}  	

tab1 ost*
  

******************************************************************************
**																			**
**			Reasonable party-identification variable						**
**																			**
******************************************************************************

foreach welle in 1 3 {
recode kp`welle'_2090 (2/3=1 "CDU/CSU") (4=4 "SPD") ///
	(5=5 "FDP") (6=6 "Greens") (7=7 "The Left") (801=801 "andere Partei") ///
	(808=808 "no party") (997 999=.), gen(pidfull`welle')
}

* Fill 2009 W2-starter variable from wave 1 with values from wave 3
replace pidfull1 = pidfull3 if w1==0

lab var pidfull1 "PID inkl. W2-Starter"

fre pidfull1
fre w1  
  
drop pidfull3  
  
 
********************************************************************************
**
** Coalition preferences (attention: different operationalization from 2013!)
**
********************************************************************************   
 
  
foreach welle of numlist 1/4 6/7 {   
   
   // Black-Yellow
   gen koasge`welle' = 0 if w`welle'==1 
   replace koasge`welle' = 1 if kp`welle'_920a==1 & kp`welle'_920c==1 & ///
      kp`welle'_920b==0 & kp`welle'_920d==0 & kp`welle'_920e==0
	replace koasge`welle' = . if kp`welle'_920a==.
   
   // Grand Coalition
   gen koagk`welle' = 0 if w`welle'==1 
   replace koagk`welle' = 1 if kp`welle'_920a==1 & kp`welle'_920b==1 & ///
      kp`welle'_920c==0 & kp`welle'_920d==0 & kp`welle'_920e==0  
   replace koagk`welle' = . if kp`welle'_920a==.
   
   // Red-Green
   gen koarg`welle' = 0 if w`welle'==1 
   replace koarg`welle' = 1 if kp`welle'_920b==1 & kp`welle'_920d==1 & ///
      kp`welle'_920a==0 & kp`welle'_920c==0 & kp`welle'_920e==0  
   replace koarg`welle' = . if kp`welle'_920a==.
   
   // Red-Red-Green
   gen koarrg`welle' = 0 if w`welle'==1 
   replace koarrg`welle' = 1 if kp`welle'_920b==1 & kp`welle'_920d==1 & ///
      kp`welle'_920e==1 & kp`welle'_920a==0 & kp`welle'_920c==0  
	replace koarrg`welle' = . if kp`welle'_920a==.  
}


* Missing wave 5: imputation of value from wave 4
/* Mean value imputation from waves 4 and 5 does not make sense, since this
variable should only take on values of 0 or 1, not 0.5 */


   foreach koal in koasge koagk koarg koarrg {
      gen `koal'5 = `koal'4
}

	  
fre koa*4 koa*5
	  

********************************************************************************
**
** Satisfaction with the government
**
******************************************************************************** 
 
// Only for waves 4 and 6

sort lfdn

drop kp4_730

merge 1:1 lfdn using "${data}\scalometer government 2009.dta"

drop if _merge == 2

drop _merge
 
 
 
foreach welle of numlist 4 6 {
   recode kp`welle'_730 (99/102=.), gen(regzufr`welle') copyrest
}
 
// Replacing missing values: Waves 1-3 --> value from 4
// Wave 5 --> mean value from 4 and 6
// Wave 7 --> Wave 6

foreach welle of numlist 1/3 {
   gen regzufr`welle' = regzufr4
}

foreach welle of numlist 5 {
   egen regzufr`welle' = rowmean(regzufr4 regzufr6)
}
 
foreach welle of numlist 7 {
   gen regzufr`welle' = regzufr6
} 



foreach welle of numlist 1/7 {
   replace regzufr`welle'=(regzufr`welle'-1)/10
}
fre regzufr?
 
********************************************************************************
**
** Evaluation scales candidates
**
********************************************************************************


foreach welle of numlist 1/7 {
   recode kp`welle'_650a (99= .) (98=6), gen(kand1`welle') copyrest 
   recode kp`welle'_650b (99= .) (98=6), gen(kand4`welle') copyrest
   capture: recode kp`welle'_650f (99= .) (98=6), gen(kand5`welle') copyrest
   capture: recode kp`welle'_650h (99= .) (98=6), gen(kand6`welle') copyrest
   capture: recode kp`welle'_650i (99= .) (98=6), gen(kand7`welle') copyrest
} 
 

// Replacing missing values: Westerwelle (collected in 1, 3, 6, 7)

foreach welle of numlist 2 {
   egen kand5`welle' = rowmean(kand51 kand53)
}

foreach welle of numlist 4 5 {
   egen kand5`welle' = rowmean(kand53 kand56)
}

// Replacing missing values: Trittin (collected in 1, 4, 7)

gen kand62 = 2/3*kand61+1/3*kand64 
replace kand62 = kand61 if kand64==. & kand61!=.
replace kand62 = kand64 if kand64!=. & kand61==.

gen kand63 = 1/3*kand61+2/3*kand64 
replace kand63 = kand61 if kand64==. & kand61!=.
replace kand63 = kand64 if kand64!=. & kand61==.

gen kand65 = 2/3*kand64+1/3*kand67 
replace kand65 = kand64 if kand67==. & kand64!=.
replace kand65 = kand67 if kand67!=. & kand64==.

gen kand66 = 1/3*kand64+2/3*kand67 
replace kand66 = kand64 if kand67==. & kand64!=.
replace kand66 = kand67 if kand67!=. & kand64==.


list kand64 kand65 kand66 kand67 if kand64!=. & kand67!=.

// Replacing missing values: Lafontaine (collected in 1, 4, 7)

gen kand72 = 2/3*kand71+1/3*kand74 
replace kand72 = kand71 if kand74==. & kand71!=.
replace kand72 = kand74 if kand74!=. & kand71==.

gen kand73 = 1/3*kand71+2/3*kand74 
replace kand73 = kand71 if kand74==. & kand71!=.
replace kand73 = kand74 if kand74!=. & kand71==.

gen kand75 = 2/3*kand74+1/3*kand77 
replace kand75 = kand74 if kand77==. & kand74!=.
replace kand75 = kand77 if kand77!=. & kand74==.

gen kand76 = 1/3*kand74+2/3*kand77 
replace kand76 = kand74 if kand77==. & kand74!=.
replace kand76 = kand77 if kand77!=. & kand74==.

// Value range

foreach welle of numlist 1/7 {
   replace kand1`welle'=(kand1`welle'-1)/10
   replace kand4`welle'=(kand4`welle'-1)/10
   replace kand5`welle'=(kand5`welle'-1)/10
   replace kand6`welle'=(kand6`welle'-1)/10
   replace kand7`welle'=(kand7`welle'-1)/10
   }

sum kand?6

********************************************************************************
**
** Competence Grading
**
********************************************************************************


foreach welle of numlist 1/7 {
foreach partei of numlist 1 4/7 {
recode kp`welle'_850 (999=.) (1000=0) (`partei'=1) ///
	(else=0), gen(first`partei'`welle')
replace first`partei'`welle' = . if kp`welle'_850==.
recode kp`welle'_870 (999=.) (1000=0) (`partei'=1) ///
	(else=0), gen(second`partei'`welle')
replace second`partei'`welle' = . if kp`welle'_870==.
}
}


tab kp7_850 first47, m

foreach welle of numlist 1/7 {
foreach partei of numlist 1 4/7 {
gen komp`partei'`welle' = 0 if first`partei'`welle'==0 & ///
	second`partei'`welle'==0
replace komp`partei'`welle' = 0.33 if first`partei'`welle'==0 & ///
	second`partei'`welle'==1
replace komp`partei'`welle' = 0.66 if first`partei'`welle'==1 & ///
	second`partei'`welle'==0
replace komp`partei'`welle' = 1 if first`partei'`welle'==1 & ///
	second`partei'`welle'==1
}
}


	  
********************************************************************************
**
** Economic Voting
**
********************************************************************************

// Individual economic situation and general economic situation (in retrospect)

foreach welle of numlist 1 4 6 7 {
   recode kp`welle'_760 (1=5) (2=4) (3=3) (4=2) (5=1) (6/105=.), ///
      gen(ewl`welle')
   recode kp`welle'_800 (1=5) (2=4) (3=3) (4=2) (5=1) (6/105=.), ///
      gen(awl`welle')
}



// Replacing missing values in waves 2, 3, 5

foreach welle of numlist 2 3 5 {
   foreach var in awl ewl  {
      gen `var'`welle' = . 
}
}   

foreach var in awl ewl {
   
   replace `var'2 = 2/3*`var'1 + 1/3*`var'4
   replace `var'2 = `var'1 if `var'4==.
   replace `var'2 = `var'4 if `var'1==.
   
   replace `var'3 = 1/3*`var'1 + 2/3*`var'4
   replace `var'3 = `var'1 if `var'4==.
   replace `var'3 = `var'4 if `var'1==.
   
   replace `var'5 = 1/2*`var'4 + 1/2*`var'6
   replace `var'5 = `var'4 if `var'6==.
   replace `var'5 = `var'6 if `var'4==.
}

// Value range

foreach welle of numlist 1/7 {
   replace awl`welle'=(awl`welle'-1)/4
   replace ewl`welle'=(ewl`welle'-1)/4
   }
   
sum awl? ewl?


********************************************************************************
**
** Save the relevant variables
**
********************************************************************************

keep lfdn pid* kand* komp* ewl? awl? regzufr? ///
   koa* 
   
save "Data\michigan09.dta", replace
